package com.liang.pcc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liang.pcc.domain.HarmonicData;
import com.liang.pcc.excel.model.MeasurementPoint;
import com.liang.pcc.mapper.MeasurementPointMapper;
import com.liang.pcc.service.HarmonicDataService;
import com.liang.pcc.service.MeasurementPointService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 实测点主表Service实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class MeasurementPointServiceImpl extends ServiceImpl<MeasurementPointMapper, MeasurementPoint> implements MeasurementPointService {

    private final HarmonicDataService harmonicService;

    @Transactional
    public void saveBatchWithHarmonics(List<MeasurementPoint> points) {
        // 批量保存监测点
        saveBatch(points);

        // 展开谐波数据并关联ID
        List<HarmonicData> harmonics = points.stream()
                .flatMap(point -> point.getHarmonics().stream()
                        .peek(hd -> hd.setPointId(point.getId()))
                ).toList();

        harmonicService.saveBatch(harmonics);
    }
}




